<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="titles" @tabClick="tabClick" ref="tabControl1"  class="tab-control" v-show="isTabFixed"/>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" @pullingUp="loadMore" :pull-up-load="true">
      <home-swiper :banner-list="bannerList" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :fixed-list="fixedList"/>
      <feature-view />
      <tab-control :titles="titles" @tabClick="tabClick" ref="tabControl2"/>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />

  </div>
</template>

<script>
  /**
  * 页面组件
  * */
  import NavBar from "components/common/navbar/NavBar"
  import HomeSwiper from "./childComps/HomeSwiper"
  import RecommendView from "./childComps/RecommendView"
  import FeatureView from "./childComps/FeatureView"

  /**
   * 公共组件
   * */
  import TabControl from "components/content/tabControl/TabControl"
  import GoodsList from "components/content/goods/GoodsList"
  import Scroll from 'components/common/scroll/Scroll'

  import {itemListenerMixin,backTopMixin} from "common/mixin";

  /**
   * 接口组件
   * */
  import {getTopdata,getProlist} from '../../network/home'




  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll
    },
    mixins: [itemListenerMixin,backTopMixin],
    data() {
      return {
        bannerList: [],
        fixedList: [],
        banners: [],
        recommends: [],
        titles:['销量','新款','价格'],
        goods: {
          'xl': {page:1, list:[], isNext:true},
          'time': {page:1, list:[], isNext:true},
          'jg': {page:1, list:[], isNext:true}
        },
        currentType: 'xl',

        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0
      }
    },
    activated() {
      // tab切换后执行
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      this.saveY = this.$refs.scroll.getScrollY()
    },
    created() {
      // 1.请求多个数据
      this.homeGetTopdata()
      // this.homeGetHomeMultidata()
      // 2.请求商品数据
      this.homeGetProlist('xl')
      this.homeGetProlist('time')
      this.homeGetProlist('jg')
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    methods: {
      /**
      * 事件监听相关
      * */
      tabClick(index) {
        const titlesArr = ['xl','time','jg'];
        this.currentType = titlesArr[index]
        this.$refs.tabControl2.currentIndex = index;
        this.$refs.tabControl1.currentIndex = index;
      },

      contentScroll(position) {
        // 监听滚动
        // 1.判断backTop是否显示
        this.isShowBackTop = position.y < -1000
        // 2.决定tabControl是否吸顶（postion:fixed）
        this.isTabFixed = (-position.y) > this.tabOffsetTop
      },
      loadMore() {
        this.homeGetProlist(this.currentType)
      },
      swiperImageLoad() {
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      },
      /**
      * 网络请求相关
      * */
      homeGetTopdata() {
        getTopdata().then((res) => {
          this.bannerList = res.banner_list
          this.fixedList = res.fixed_list
        })
      },
      // homeGetHomeMultidata() {
      //   getHomeMultidata().then(res => {
      //     // this.result = res;
      //     this.banners = res.data.banner.list;
      //     this.recommends = res.data.recommend.list;
      //   })
      // },
      homeGetProlist(type) {
        if (!this.goods[type].isNext){
          this.$refs.scroll.finishPullUp()
          return false
        }
        const page = this.goods[type].page
        getProlist(type,page).then(res => {
          // ... 将数组中的元素一个个塞进数组中
          this.goods[type].list.push(...res.prolist)
          this.goods[type].page += 1
          // 完成上拉加载更多
          this.$refs.scroll.finishPullUp()
          if (res.prolist.length < 20){
            this.goods[type].isNext = false;
          }
        })
      }
    },
    mounted() {


    }
  }
</script>

<style scoped>
  #home {
    /*padding-top: 44px;*/
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;

    /*在使用浏览器原生滚动时, 为了让导航不跟随一起滚动*/
    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 9;*/
  }

  .content {
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }


  /*.content {*/
  /*height: calc(100% - 93px);*/
  /*overflow: hidden;*/
  /*margin-top: 44px;*/
  /*}*/

</style>
